package com.dc.web.controller;


import com.dc.dao.CalibrationMachinePipeDao;
import com.dc.dao.ShipDao;
import com.dc.entity.CalibrationMachinePipeEntity;
import com.dc.entity.ShipEntity;
import com.dc.model.AjaxResult;
import com.dc.model.PageListBody;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 3-法兰，弯头两种机电管子的处理类，逻辑一直，type区分，界面有两个
 */
@Component
public class CalibrationMachinePipeController {

    @Resource
    CalibrationMachinePipeDao calibrationMachinePipeDao;

    @Resource
    ShipDao shipDao;

    String type ;

    //    @GetMapping("/list")
    public AjaxResult list(PageListBody body) {

        Integer pageNumber = body.getPageNumber();
        pageNumber = ObjectUtils.defaultIfNull(pageNumber, 1);
        Integer pageSize = body.getPageSize();
        pageSize = ObjectUtils.defaultIfNull(pageSize, 20);

        PageRequest pageRequest = PageRequest.of(pageNumber - 1, pageSize);

        CalibrationMachinePipeEntity entity = new CalibrationMachinePipeEntity();
        entity.setType(type);
        Example example = Example.of(entity);
        Page<CalibrationMachinePipeEntity> all = calibrationMachinePipeDao.findAll(example, pageRequest);
        all.getContent().forEach(t-> {
            t.setShipId(t.getShip().getId());
            t.setShipName(t.getShip().getName());
        });

        return AjaxResult.success(all);
    }

//    @RequestMapping("/{id}")
    public AjaxResult show(@PathVariable("id") Long id) {

        Optional<CalibrationMachinePipeEntity> optional = calibrationMachinePipeDao.findById(id);
        CalibrationMachinePipeEntity entity = null;
        if (optional.isPresent()) {
            entity = optional.get();
            entity.setShipId(entity.getShip().getId());
            entity.setShipName(entity.getShip().getName());
        }

        return AjaxResult.success(entity);
    }

//    @PostMapping()
    public AjaxResult add(@RequestBody CalibrationMachinePipeEntity entity) {
        Optional<ShipEntity> optional = shipDao.findById(entity.getShipId());
        if (optional.isPresent()) {
            entity.setId(null);
            entity.setShip(optional.get());
            // 动态计算
            calc(entity);

            CalibrationMachinePipeEntity save = calibrationMachinePipeDao.save(entity);
            return AjaxResult.success(save);
        }
        return AjaxResult.error("保存失败");
    }

//    @PutMapping("/{id}")
    public AjaxResult update(@PathVariable("id") Long id, @RequestBody CalibrationMachinePipeEntity entity) {

        Optional<ShipEntity> optional = shipDao.findById(entity.getShipId());

        if (optional.isPresent()) {
            ShipEntity ship = optional.get();
            entity.setId(id);
            entity.setShip(ship);
            // 动态计算
            calc(entity);

            CalibrationMachinePipeEntity save = calibrationMachinePipeDao.save(entity);
            save.setShipId(ship.getId());
            save.setShipName(ship.getName());
            return AjaxResult.success(save);
        }
        return AjaxResult.error("保存失败");
    }

//    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        calibrationMachinePipeDao.deleteById(id);

        return AjaxResult.success();
    }

    private CalibrationMachinePipeEntity calc(CalibrationMachinePipeEntity entity) {
        // length = gapNum * pi * d
        try {
            double gapNum = entity.gapNum == null ? 0. : entity.gapNum;
            double d = entity.d == null ? 0. : entity.d;
            entity.length = gapNum * Math.PI * d;
        } catch (Exception e) {
            entity.length = 0.;
        }

        // powerByUnit = power * 1000 / length
        try {
            double power = entity.power == null ? 0. : entity.power;
            entity.powerByUnit = power * 1000 / entity.length;
        } catch (Exception e) {
            entity.powerByUnit = 0.;
        }

        return entity;
    }


}
